package com.luckcoder.relax.games.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.luckcoder.relax.games.domain.bo.RelaxAudioConfigBo;
import com.luckcoder.relax.games.domain.vo.RelaxAudioConfigVo;
import com.luckcoder.relax.games.domain.RelaxAudioConfig;
import com.luckcoder.relax.games.mapper.RelaxAudioConfigMapper;
import com.luckcoder.relax.games.service.IRelaxAudioConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 音效配置Service业务层处理
 *
 * @author zhanghao
 * @date 2025-09-11
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RelaxAudioConfigServiceImpl implements IRelaxAudioConfigService {

    private final RelaxAudioConfigMapper baseMapper;

    /**
     * 查询音效配置
     *
     * @param audioId 主键
     * @return 音效配置
     */
    @Override
    public RelaxAudioConfigVo queryById(Long audioId){
        return baseMapper.selectVoById(audioId);
    }

    /**
     * 分页查询音效配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 音效配置分页列表
     */
    @Override
    public TableDataInfo<RelaxAudioConfigVo> queryPageList(RelaxAudioConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RelaxAudioConfig> lqw = buildQueryWrapper(bo);
        Page<RelaxAudioConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的音效配置列表
     *
     * @param bo 查询条件
     * @return 音效配置列表
     */
    @Override
    public List<RelaxAudioConfigVo> queryList(RelaxAudioConfigBo bo) {
        LambdaQueryWrapper<RelaxAudioConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RelaxAudioConfig> buildQueryWrapper(RelaxAudioConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RelaxAudioConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(RelaxAudioConfig::getAudioId);
        lqw.eq(StringUtils.isNotBlank(bo.getGameCode()), RelaxAudioConfig::getGameCode, bo.getGameCode());
        lqw.like(StringUtils.isNotBlank(bo.getAudioName()), RelaxAudioConfig::getAudioName, bo.getAudioName());
        lqw.eq(StringUtils.isNotBlank(bo.getAudioType()), RelaxAudioConfig::getAudioType, bo.getAudioType());
        lqw.eq(StringUtils.isNotBlank(bo.getAudioUrl()), RelaxAudioConfig::getAudioUrl, bo.getAudioUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getLocalPath()), RelaxAudioConfig::getLocalPath, bo.getLocalPath());
        lqw.eq(bo.getVolume() != null, RelaxAudioConfig::getVolume, bo.getVolume());
        lqw.eq(bo.getDuration() != null, RelaxAudioConfig::getDuration, bo.getDuration());
        lqw.eq(StringUtils.isNotBlank(bo.getFileFormat()), RelaxAudioConfig::getFileFormat, bo.getFileFormat());
        lqw.eq(StringUtils.isNotBlank(bo.getFallbackType()), RelaxAudioConfig::getFallbackType, bo.getFallbackType());
        lqw.eq(bo.getWeight() != null, RelaxAudioConfig::getWeight, bo.getWeight());
        lqw.eq(bo.getSortOrder() != null, RelaxAudioConfig::getSortOrder, bo.getSortOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), RelaxAudioConfig::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getIsEnabled()), RelaxAudioConfig::getIsEnabled, bo.getIsEnabled());
        return lqw;
    }

    /**
     * 新增音效配置
     *
     * @param bo 音效配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(RelaxAudioConfigBo bo) {
        RelaxAudioConfig add = MapstructUtils.convert(bo, RelaxAudioConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAudioId(add.getAudioId());
        }
        return flag;
    }

    /**
     * 修改音效配置
     *
     * @param bo 音效配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(RelaxAudioConfigBo bo) {
        RelaxAudioConfig update = MapstructUtils.convert(bo, RelaxAudioConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RelaxAudioConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除音效配置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
